package com.atguigu.stack;

import java.lang.reflect.Proxy;
import java.nio.channels.Pipe;
import java.util.Scanner;
import java.util.Stack;

/**
 * @author shkstart
 * @create 2021-09-20 9:59
 */
public class Stackpipei {
    public static void piperi(String expression){
        if (expression==""||expression==null){
            System.out.println("输入的表达式为空！");
        }
        //创建一个栈
        Stack<Character> stack = new Stack<>();
        for (int i=0;i<expression.length();i++){
            switch (expression.charAt(i)){
                case '(':stack.push(expression.charAt(i));
                    break;
                case '{':stack.push(expression.charAt(i));
                    break;
                case ')':
                    if (!stack.empty()&&stack.peek()=='('){
                        stack.pop();
                    }
                    break;
                case '}':
                    if (!stack.empty()&&stack.peek()=='{'){
                        stack.pop();
                    }
                    break;
            }
        }
        if (stack.empty()){
            System.out.println("匹配成功");
        }else {
            System.out.println("匹配失败");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要判断的式子：");
        String express = scanner.next();
        piperi(express);
    }
}
